<template>
    <div class="dahezi">
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item >🏠</el-breadcrumb-item>
            <el-breadcrumb-item>病友论坛</el-breadcrumb-item>
            <el-breadcrumb-item>帖子详情</el-breadcrumb-item>
        </el-breadcrumb>

        <div class="hezi">
            <div class="hezi2">
                <div class="hezi3">
                <div class="classneirong">
                    {{tiezi.classificationName}}
                </div>

                <div class="user">
                    <router-link :to="{name:'yonghuxinxichaxun',
                                 query:{id:tiezi.userId
                                 }}">
                    <img :src="tiezi.avatar"  style="width:150px; height:150px">
                  <div style="text-align: center">博主：{{tiezi.userName}}</div>
                    </router-link>
                </div>
                <div class="tiezineirong">
                    <div class="content1">
                        <div v-html="tiezi.content" />
                    </div>
                    <div class="timecss">
                        <span style="margin-left: 30px; color: #888888; float: right">
                        发布时间：{{tiezi.time}}
                        </span>
                        <el-button  v-if="form.userId" @click="dianji" type="primary" size="mini" style="margin:4px ; float: right; margin-right: 80px" >
                            回复({{length1}})
                        </el-button>
                        <el-button v-if="tiezi.classificationName=='健康计划'" @click="qiandao"  type="success" size="mini" style="margin:4px ; float: right; margin-right: 80px" >
                            签到
                        </el-button>
                    </div>
                    </div>


                <div v-if="open">
<!--                    <div class="huifukuan">-->
                        <div class="huifukuan1">
                            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                                <el-form-item >
                                    <el-input type="textarea" v-model="form.content" :min-height="80"/>
                                </el-form-item>
                            </el-form>
                        </div>
                            <el-button type="primary" size="mini" style="margin-left: 650px;" @click="submitForm">发表</el-button>

<!--                    </div>-->
                </div>
<!--               回复区域-->
                <div class="pinglunqu">
                <div style="margin-left: 50px; font-size: 28px"> 评论区：</div>

                    <div  style="margin-top: 10px"  v-for="(q,index) in tiezichild" :key="index">
                        <div class="rediang2">
                            <ComponentItem :comment="q"/>
                        </div>
                    </div>
                </div>


            </div>
            </div>
        </div>


        <!-- 分享到论坛 -->
        <el-dialog :visible.sync="visible" :title="title" width="500px" append-to-body>
            <el-form ref="issueForm" :model="issueForm" :rules="rules" label-width="80px">
                <el-form-item  prop="content">
                    <el-input type="textarea" v-model="issueForm.content" :min-height="500"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitIssueForm">发布</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import marked from "marked";
    import hljs from "highlight.js";
    import "highlight.js/styles/monokai-sublime.css";
    import ComponentItem from "./CommentItem.vue";
    export default {
        name: "tiezixiangqing",
        components: {
            ComponentItem},
        data(){
            return{
                classificationList: null,
                tiezi:{},
                tiezichild:[],
                length1:0,
                form:{
                    fatherId:'',
                    userId:'',
                    content:''
                },
                open:false,
                title: null,
                // 表单参数
                issueForm: {},
                // 表单校验
                rules: {
                },
                visible: false,
                msg: '',
            }
        },
        mounted() {
            //初始化
            this.tiezixiangqing();
            marked.setOptions({
                renderer: new marked.Renderer(),
                highlight: function (code) {
                    return hljs.highlightAuto(code).value;
                },
                pedantic: false,
                gfm: true,
                tables: true,
                breaks: true,
                sanitize: false,
                smartLists: true,
                smartypants: false,
                xhtml: true,
                msgContent: '',
            });
        },
        created() {
            this.tiezixiangqing()
            this.checkLogin()
        },
        methods:{
            // 分享框
            openShare() {
                this.$confirm(this.msg+'是否分享到论坛，鼓励自己', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'success'
                }).then(() => {
                    this.issueForm.classificationId = 4;
                    this.issueForm.fatherId = this.tiezi.id;
                    this.issueForm.content = "我已经"+this.msgContent+"快和我一起吧";
                    this.visible = !this.visible;
                    this.title = '健康打卡';
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消'
                    });
                });
            },
          async  tiezixiangqing(){

              this.form.fatherId=this.$route.query.id
                const {data:jieguo}=await this.$http.get(`topic/${this.$route.query.id}`)
              this.tiezi=jieguo.data.data.topic
              this.tiezichild=jieguo.data.data.child
              this.length1=  this.tiezichild.length
              this.reload();
            },
            qiandao(){
                const token = sessionStorage.getItem("token");
                this.$http.defaults.headers.common['Authorization'] = token;
                this.$http.get(`/topic/signIn?topicId=${this.$route.query.id}`).then(response => {
                    console.log(response)
                    if (response.data.code === 200) {
                        this.msg = response.data.data;
                        this.msgContent = response.data.data;
                        this.openShare();
                    }
                });
            },
            dianji(){
                this.getgerenziliao();
                if(this.form.userId != ''){
                    this.open=!this.open;
                    this.form.fatherId = this.tiezi.id;
                }
            },
            /** 发表按钮 */
            submitForm() {
                console.log(this.form);
                if (this.form.fatherId != null) {
                    const token = sessionStorage.getItem("token");
                    this.$http.defaults.headers.common['Authorization'] = token;
                    console.log(this.form)
                    this.$http.post('/topic', this.form).then(response => {
                        console.log(response)
                        if (response.data.code === 200) {
                            this.$message({showClose: true, message: "回复成功！", type: "success"})
                            this.open = false;
                            this.tiezixiangqing()
                        }
                    });
                }
            },
            submitIssueForm(){
                console.log(this.issueForm);
                if (this.issueForm.fatherId != null) {
                    this.issueForm.classificationId=4;
                    const token = sessionStorage.getItem("token");
                    this.$http.defaults.headers.common['Authorization'] = token;
                    console.log(this.form)
                    this.$http.post('/topic', this.issueForm).then(response => {
                        console.log(response)
                        if (response.data.code === 200) {
                            this.$message({showClose: true, message: "发布成功！", type: "success"})
                            this.visible = false;
                            this.tiezixiangqing()
                        }
                    });
                }
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form.content=''
            },
            checkLogin(){
                const token = sessionStorage.getItem("token");
                console.log(token);
                if(token != null){
                    this.getgerenziliao();
                }
            },
            async getgerenziliao(){
                const {data:jieguo}=await this.$http.get('user/info',{headers:{
                        'Authorization':window.sessionStorage.getItem('token')
                    }})
                this.form.userId=jieguo.data.basicUserInformation.id
              },
            // 查看帖子分类
            async getClassName(){
                this.loading = true;
                const token = sessionStorage.getItem("token");
                this.$http.defaults.headers.common['Authorization'] = token;
                const {data: response} = await this.$http.get('classification/list');
                this.classificationList = response.data;
                this.loading = false;
            },

        }
    }
</script>

<style scoped>
    .hezi{
        width: 80%;
        height: 700px;
        height: auto!important;
        display: flex;
        justify-content:center;
        /*align-items:center;*/
    }
    .hezi2{
        color: black;
        width: 100%;
        background-color: white;
        height: 650px;
        margin-top: 3px;
        overflow: scroll;
        /*display: flex;*/
        /*justify-content:center;*/
    }
    .dahezi{
        padding-top: 3px;
        background-color: #fffacd;
    }
    .tiezineirong{
        border: #EEE8AA solid 2px;
        width: 70%;
        /*height: 80%;*/
        overflow: hidden;
        text-overflow: ellipsis;
        margin-left: 30px;
        background: #FFFFE0;
        min-height: 200px;
    }
    .content1{
        width: 100%;
        min-height: 180px;
    }
    .timecss{
        float: bottom;
    }

    .user{
        /*border: #888888 solid 2px;*/
        float: right;
        margin-right: 30px;
        margin-top: 30px;
        z-index: 1;
    }
    .classneirong{
        /*background-color: #F5F5DC;*/
        width: 75%;
        height: 35px;
        margin: 6px;
        margin-left: 30px;
        padding: 2px;
        /*border: solid 1px #EEE8AA;*/
        text-align: center;
        color: black;
        line-height: 2em
    }
    .rediang2{
        width: 65%;
        margin-left: 30px;
    }
    .huifukuan1{
        width: 70%;
        height: 60px;
        margin-left: 30px;
    }
    .pinglunqu{
        width: 100%;
        background: #F5FFEE;
        min-height: 600px;
        height: auto!important;
    }
</style>
